package com.honvay.cola.cloud.auth.client.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.honvay.cola.cloud.auth.client.entity.Client;
import com.honvay.cola.cloud.auth.client.service.ClientService;
import com.honvay.cola.cloud.framework.base.controller.BaseController;
import com.honvay.cola.cloud.framework.core.protocol.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * OAuth2客户端 控制器
 * </p>
 *
 * @author LIQIU
 * @since 2018-04-13
 */
@RestController
@RequestMapping("/client/client")
@Api(tags = "OAuth2客户端 接口")
public class ClientController extends BaseController {

    @Autowired
    private ClientService clientService;

    /**
     * 获取OAuth2客户端列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取OAuth2客户端列表")
    public Result<Page<List<Client>>> list(
            @RequestParam(value = "grantType", defaultValue = "", required = false) String grantType,
            @RequestParam(value = "clientId", defaultValue = "", required = false) String clientId
//            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
//            @RequestParam(value = "pageNumber", defaultValue = "1", required = false) int pageNumber,
//            @RequestParam(value = "sort", defaultValue = "", required = false) String sort
    ) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.like("grant_type", grantType);
        wrapper.like("client_id", clientId);
        return this.success(clientService.selectPage(this.getPagination(), wrapper));
    }

    /**
     * 新增OAuth2客户端
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增OAuth2客户端")
    public Result<String> add(Client client) {
        clientService.insert(client);
        return this.success();
    }

    /**
     * 删除OAuth2客户端
     */
    @PostMapping(value = "/delete/{id}")
    @ApiOperation("删除OAuth2客户端")
    public Result<String> delete(@PathVariable("id") Long id) {
        clientService.deleteById(id);
        return this.success();
    }

    /**
     * 修改OAuth2客户端
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改OAuth2客户端")
    public Result<String> update(Client client) {
        clientService.updateById(client);
        return this.success();
    }

    /**
     * 获取OAuth2客户端
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取OAuth2客户端")
    public Result<Client> get(@PathVariable("id") Long id) {
        return this.success(clientService.selectById(id));
    }
}

